package com.yy.job.core.domain.converter;

import com.alibaba.fastjson.JSON;
import com.yy.job.core.domain.JobResult;
import com.yy.job.core.domain.JobStepDomain;
import com.yy.job.core.domain.JobStepExecLogDomain;
import com.yy.job.core.domain.context.JobContext;
import com.yy.job.core.domain.context.JobStepContext;

public class JobStepExecLogConverter {
	
	public static JobStepExecLogDomain buildStepExecLog(JobContext jobContext, JobStepContext jobStepContext) {
		
		JobStepDomain jobStepDomain = jobStepContext.getJobStepDomain();
		JobStepExecLogDomain stepExecLogDomain = new JobStepExecLogDomain();
		stepExecLogDomain.setJobId(jobStepDomain.getJobId());
		stepExecLogDomain.setJobLogId(jobContext.getJobLogId());
		stepExecLogDomain.setJobStepId(jobStepDomain.getId());
		stepExecLogDomain.setExecStartTime(jobStepContext.getExecStartTime());
		stepExecLogDomain.setExecEndTime(jobStepContext.getExecEndTime());
		stepExecLogDomain.setOperator(jobContext.getOperator());
		
		// 处理日志信息
		JobResult jobResult = jobStepContext.getJobResult();
		if (jobResult.isSuccess()) {
			stepExecLogDomain.setJobStepResultFlag("Y");
			stepExecLogDomain.setJobStepResult(JSON.toJSONString(jobResult));
		} else {
			stepExecLogDomain.setJobStepResultFlag("N");
			stepExecLogDomain.setErrMsg(jobResult.getErrorMsg());
			stepExecLogDomain.setErrStack(jobResult.getErrStack());
		}
		return stepExecLogDomain;
	}

}
